package com.huowenjie.service;

import com.huowenjie.dao.UserDao;
import com.huowenjie.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl  implements  UserService{


    @Autowired
    UserDao userDao;


    @Override
    public void add(User user) {
        userDao.saveAndFlush(user);
    }

    @Override
    public List<User> userList() {
        return userDao.findAll();
    }

    @Override
    public Page<User> feny(Integer pageNum, Integer pageSize) {
        PageRequest of = PageRequest.of(pageNum, pageSize);
        return userDao.findAll(of);
    }

    @Override
    public Page<User> fenyd(Integer pageNum, Integer pageSize, Sort sort) {
        PageRequest of = PageRequest.of(pageNum, pageSize,sort);
        return userDao.findAll(of);
    }

    @Override
    public User byid(Long id) {
        return userDao.findById(id).get();
    }

    @Override
    public void del(Long id) {
        userDao.deleteById(id);
    }

    @Override
    public void update(User user) {
        userDao.saveAndFlush(user);
    }

    @Override
    public List<User> findBySexEquals(String sex) {
        return userDao.findBySexEquals(sex);
    }

    @Override
    public List<User> findByNameLike(String name) {
        return userDao.findByNameLike("%"+name+"%");
    }
}
